using System;
using System.IO;
using System.ComponentModel;
namespace Lizk.SimpleHUD
{
    [Serializable]
    public class HUDImage : HUDControl
    {
        protected string file;

        [NonSerialized]
        protected bool reload;
        
        protected bool constrain = true;
        protected ColorS imageColor = ColorS.White;
        protected RectangleS imageBounds;
        private bool fadeOnLoad;

        public bool FadeOnLoad
        {
            get { return fadeOnLoad; }
            set { fadeOnLoad = value; }
        }
        public string File { get { return file; } set { if (file != value) ImageSize = RectangleS.Empty;  file = value; } }
        public bool ConstrainProportions { get { return constrain; } set { constrain = value; } }
        public ColorS Color { get { return imageColor; } set { imageColor = value; } }
#if !XBOX
    [Browsable(false)]
#endif
        public bool ReloadImage { get { return reload; } set { reload = value; } }
#if !XBOX
        [Browsable(false)]
#endif
        public RectangleS ImageSize { get { return imageBounds; } set { RectangleS oldBounds = imageBounds; imageBounds = value; if (oldBounds != value && ImageSizeChanged != null) ImageSizeChanged(this, EventArgs.Empty); } }

        public event EventHandler ImageSizeChanged;

        public HUDImage() { }

        public HUDImage(string file)
        {
            this.file = file;
        }

        public static RectangleS ResizeConstrained(bool constrain, SizeS size, SizeS canvas)
        {
            RectangleS rect = new RectangleS();

            float cProp = canvas.Width/canvas.Height;
            double sProp = size.Width/size.Height;
            double propDiff = Math.Abs(cProp - sProp);

            if (cProp > sProp && constrain)
            {
                rect.Height = canvas.Height;
                rect.Width = canvas.Height/size.Height*size.Width;
                rect.X = (canvas.Width - rect.Width)/2f;
            }
            else if (cProp < sProp && constrain)
            {
                rect.Width = canvas.Width;
                rect.Height = canvas.Width/size.Width*size.Height;
                rect.Y = (canvas.Height - rect.Height)/2f;
            }
            else
            {
                rect.Width = canvas.Width;
                rect.Height = canvas.Height;
            }
            return rect;
        }
    }
}